# coding:utf-8
import json
from .. import models
import time
from .gain_data import GainData
from ..celery_tasks import send_email_task, timing_update_data


class CountsData:
    def __init__(self):
        self.tokens = lambda token: True if int(token) + 86400 > int(time.time()) else False
        self.start_date = '0000-00-00 00:00:00'
        self.end_date = '9999-00-00 00:00:00'
        timing_update_data.delay()

    # # 查询APP 功能测试用例总数
    def app_count(self, request):
        if request.POST:
            try:
                if self.tokens(request.POST['token']):
                    appfunction_count_data = GainData().get_counts_data('app_count')
                else:
                    appfunction_count_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
            except:
                appfunction_count_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
        else:
            appfunction_count_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return appfunction_count_data

    # 查询APP，WEB，接口功能测试用例总数
    def fun_counts(self, request):
        if request.POST:
            try:
                if self.tokens(request.POST['token']):
                    fun_counts_data = GainData().get_counts_data('all_count')
                else:
                    fun_counts_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
            except:
                fun_counts_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})

        else:
            fun_counts_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return fun_counts_data

    # 查询WEB 功能测试用例总数
    def web_fun_count(self, request):
        if request.POST:
            try:
                if self.tokens(request.POST['token']):
                    web_fun_count_data = GainData().get_counts_data('web_count')
                else:
                    web_fun_count_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
            except:
                web_fun_count_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})

        else:
            web_fun_count_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})

        return web_fun_count_data

    # 查询接口 测试用例总数
    def interface_count(self, request):
        if request.POST:
            try:
                if self.tokens(request.POST['token']):
                    interface_count_data = GainData().get_counts_data('interface_count')
                else:
                    interface_count_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
            except:
                interface_count_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})

        else:
            interface_count_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return interface_count_data

    # 获取远程ip
    def get_remote_ip(self, request):
        if request.POST:
            try:
                if self.tokens(request.POST['token']):
                    remote_ips_list = []  # 存放ip的列表
                    remote_ips = models.automation_remote_ip.objects.all()
                    for ips in remote_ips:
                        if ips.status == "1":
                            remote_ips_list.append(ips.ipaddress)
                    get_remote_ip_data = json.dumps({"code": "200", "msg": "success", "data": remote_ips_list})
                else:
                    get_remote_ip_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
            except:
                get_remote_ip_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
        else:
            get_remote_ip_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return get_remote_ip_data

    # 发送邮件
    def send_email(self, request):
        if request.POST:
            try:
                if self.tokens(request.POST['token']):
                    send_email_task.delay(request.POST['runtime'], request.POST['case_amount'], request.POST['pass_case_count'], request.POST['not_run_case'],
                                          request.POST['event_id'])
                    send_email_data = json.dumps({"code": "200", "msg": "success", "data": ''})
                else:
                    send_email_data = json.dumps({"code": "-11", "msg": "token过期", "data": {}})
            except:
                send_email_data = json.dumps({"code": "-13", "msg": "查询数据出错，请检查参数。", "data": {}})
        else:
            send_email_data = json.dumps({"code": "-12", "msg": "请求方式错误", "data": {}})
        return send_email_data
